package com.zg.indiv.web;

import cn.hutool.core.bean.BeanUtil;
import com.zg.common.core.R;
import com.zg.common.core.dao.indiv.entity.IndivFolder;
import com.zg.indiv.constants.IndivConstant;
import com.zg.indiv.domain.bo.IndivFolderListDto;
import com.zg.indiv.domain.req.folder.*;
import com.zg.indiv.domain.vo.IndivFolderVo;
import com.zg.indiv.service.IndivFolderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.validation.Valid;

/**
* @author lixin
*/
@Api(tags = {"目录管理"})
@RestController
@RequiredArgsConstructor
public class IndivFolderController {
	private final IndivFolderService indivFolderService;

	@ApiOperation("新增首层目录")
	@ApiResponses({
			@ApiResponse(code = 809001003, message = "请选择正确的空间"),
			@ApiResponse(code = 809002001, message = "目录名称已被使用")
	})
	@PostMapping("/folder/addFirst")
	public R<Void> addFirst(@RequestBody @Valid IndivFolderAddFirstReq param) {
		indivFolderService.addFirst(param);
		return R.success();
	}

	@ApiOperation("新增下一层目录")
	@ApiResponses({
			@ApiResponse(code = 809002002, message = "请选择正确的目录"),
			@ApiResponse(code = 809002003, message = "目录中存在数据类型不能新增下一级目录")
	})
	@PostMapping("/folder/addNext")
	public R<Void> addNext(@RequestBody @Valid IndivFolderAddNextReq param) {
		indivFolderService.addNext(param);
		return R.success();
	}

	@ApiOperation("查看首层目录")
	@PostMapping("/folder/listFirst")
	public R<List<IndivFolderVo>> listFirst(@RequestBody @Valid IndivFolderListFirstReq param) {
		IndivFolderListDto indivFolderListDto = new IndivFolderListDto();
		indivFolderListDto.setSpaceId(param.getSpaceId());
		indivFolderListDto.setParentId(IndivConstant.INDIV_FOLDER_PARENT_ID);
		List<IndivFolder> list = indivFolderService.list(indivFolderListDto);
		return R.success(BeanUtil.copyToList(list, IndivFolderVo.class));
	}

	@ApiOperation("查看下一层目录")
	@PostMapping("/folder/listNext")
	public R<List<IndivFolderVo>> listNext(@RequestBody @Valid IndivFolderListNextReq param) {
		IndivFolderListDto indivFolderListDto = new IndivFolderListDto();
		indivFolderListDto.setParentId(param.getFolderId());
		List<IndivFolder> list = indivFolderService.list(indivFolderListDto);
		return R.success(BeanUtil.copyToList(list, IndivFolderVo.class));
	}

	@ApiOperation("删除目录")
	@ApiResponses({
			@ApiResponse(code = 809002004, message = "目录中存在下级目录不能删除"),
			@ApiResponse(code = 809002005, message = "目录中存在数据类型不能删除")
	})
	@PostMapping("/folder/remove")
	public R<Void> remove(@RequestBody List<Long> idList) {
		indivFolderService.remove(idList);
		return R.success();
	}

	@ApiOperation("重命名目录")
	@ApiResponses({
			@ApiResponse(code = 809002002, message = "请选择正确的目录"),
			@ApiResponse(code = 809002001, message = "目录名称已被使用")
	})
	@PostMapping("/folder/editName")
	public R<Void> editName(@RequestBody @Valid IndivFolderEditNameReq param) {
		indivFolderService.editName(param);
		return R.success();
	}

	@ApiOperation("复制目录及文件到某个目录")
	@ApiResponses({
			@ApiResponse(code = 809002006, message = "请选择正确的复制目录"),
			@ApiResponse(code = 809002007, message = "请选择正确的目标目录"),
			@ApiResponse(code = 809002008, message = "目录不在同一空间下无法进行复制"),
			@ApiResponse(code = 809002009, message = "目标目录下已存在相同的目录名称")
	})
	@PostMapping("/folder/executeCopy")
	public R<Void> executeCopy(@RequestBody @Valid IndivFolderExecuteCopyReq param) {
		indivFolderService.executeCopy(param);
		return R.success();
	}

	@ApiOperation("剪切或移动目录及文件到某个目录")
	@ApiResponses({
			@ApiResponse(code = 809002010, message = "请选择正确的移动目录"),
			@ApiResponse(code = 809002007, message = "请选择正确的目标目录"),
			@ApiResponse(code = 809002011, message = "目录不在同一空间下无法进行移动"),
			@ApiResponse(code = 809002009, message = "目标目录下已存在相同的目录名称")
	})
	@PostMapping("/folder/executeMove")
	public R<Void> executeMove(@RequestBody @Valid IndivFolderExecuteMoveReq param) {
		indivFolderService.executeMove(param);
		return R.success();
	}

	@ApiOperation("给目录打标签")
	@ApiResponses({
			@ApiResponse(code = 809002002, message = "请选择正确的目录")
	})
	@PostMapping("/folder/executeBindTag")
	public R<Void> executeBindTag(@RequestBody @Valid IndivFolderExecuteBindTagReq param) {
		indivFolderService.executeBindTag(param);
		return R.success();
	}

	@ApiOperation("绑定目录数据")
	public R<Void> executeBindData(@RequestBody @Valid IndivFolderExecuteBindDataReq param) {
		indivFolderService.executeBindData(param);
		return R.success();
	}
}